

let Action = require('./action');
// let cfg = require('../utils/cfg');

let MoveBy = require('./move').MoveBy;
let ScaleTo = require('./scale').ScaleTo;
let RotateBy = require('./rotate').RotateBy;

class Spawn extends Action{
    constructor(actions) {
        super();

        // check
        this._check(actions);

        this._actions = actions;

        let d = this._actions[0].duration;
        for(let i = 1,len = actions.length; i < len; ++i){
            if(d < actions[i].duration){
                d = actions[i].duration;
            }
        }
        this.duration = d;
    }

    start() {
        super.start();
        this._actions.forEach(action=>{
            action.target = this.target;
            action.start();
        });
    }

    update(t) {
        if(this.target.visible && this.target.enabled){
            this._actions.forEach(action=>{
                if(!action.isDone()){
                    action.update(t);
                }
            })
        }
    }

    _check(actions){
        if(actions.length < 2){
            throw new Error(`actions 至少为2个!`);
        }

        let action = actions.find(action=>{
            let isMove =  (action instanceof MoveBy);
            let isScale = (action instanceof ScaleTo);
            let isRotate = (action instanceof RotateBy);
            return !isMove && !isScale && !isRotate;
        });

        if(action){
            throw new Error(`action:${action} is null Move or Scale or Rotate Action`);
        }
    }
}

module.exports = Spawn;